package com.freight.core.common.aop;
import com.alibaba.fastjson.JSONObject;
import com.freight.core.common.annotation.LogParam;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

/**
 * @Description: 记录入参出参方法
 * @Param:
 * @return:
 **/
@Component
@Aspect
@Slf4j
public class LogParamAop {
    @Pointcut("execution(* com.freight.*.service.*.*(..))")
    public void point() {
    }

    @Around("@annotation(logParam)")
    public Object doAround(ProceedingJoinPoint joinPoint, LogParam logParam) {
        long startTime = System.currentTimeMillis();
        Object obj = null;
        //
        try {
            obj = joinPoint.proceed();
        } catch (Throwable e) {
            log.info(e.getMessage());
        }
        long endTime = System.currentTimeMillis();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        String methodName = signature.getDeclaringTypeName() + "." + signature.getName();
        log.info("LogParamAop == doAround==" + "method [" + methodName + "]  spend time: " + (endTime - startTime) + "ms");
        log.info("inParam:" + JSONObject.toJSONString(joinPoint.getArgs()));
        log.info("outParam:" + (obj != null ? obj.toString() : "null"));
        return obj;
    }

}